home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Packager.php < prev    next >
PHP Script  |  2004-10-01  |  6KB  |  166 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Packager.php,v 1.52 2004/01/08 17:33:12 sniper Exp $
  21.  
  22. require_once 'PEAR/Common.php';
  23. require_once 'System.php';
  24.  
  25. /**
  26.  * Administration class used to make a PEAR release tarball.
  27.  *
  28.  * TODO:
  29.  *  - add an extra param the dir where to place the created package
  30.  *
  31.  * @since PHP 4.0.2
  32.  * @author Stig Bakken <ssb@php.net>
  33.  */
  34. class PEAR_Packager extends PEAR_Common
  35. {
  36.     // {{{ constructor
  37.  
  38.     function PEAR_Packager()
  39.     {
  40.         parent::PEAR_Common();
  41.     }
  42.  
  43.     // }}}
  44.     // {{{ destructor
  45.  
  46.     function _PEAR_Packager()
  47.     {
  48.         parent::_PEAR_Common();
  49.     }
  50.  
  51.     // }}}
  52.  
  53.     // {{{ package()
  54.  
  55.     function package($pkgfile = null, $compress = true)
  56.     {
  57.         // {{{ validate supplied package.xml file
  58.         if (empty($pkgfile)) {
  59.             $pkgfile = 'package.xml';
  60.         }
  61.         // $this->pkginfo gets populated inside
  62.         $pkginfo = $this->infoFromDescriptionFile($pkgfile);
  63.         if (PEAR::isError($pkginfo)) {
  64.             return $this->raiseError($pkginfo);
  65.         }
  66.  
  67.         $pkgdir = dirname(realpath($pkgfile));
  68.         $pkgfile = basename($pkgfile);
  69.  
  70.         $errors = $warnings = array();
  71.         $this->validatePackageInfo($pkginfo, $errors, $warnings, $pkgdir);
  72.         foreach ($warnings as $w) {
  73.             $this->log(1, "Warning: $w");
  74.         }
  75.         foreach ($errors as $e) {
  76.             $this->log(0, "Error: $e");
  77.         }
  78.         if (sizeof($errors) > 0) {
  79.             return $this->raiseError('Errors in package');
  80.         }
  81.         // }}}
  82.  
  83.         $pkgver = $pkginfo['package'] . '-' . $pkginfo['version'];
  84.  
  85.         // {{{ Create the package file list
  86.         $filelist = array();
  87.         $i = 0;
  88.  
  89.         foreach ($pkginfo['filelist'] as $fname => $atts) {
  90.             $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
  91.             if (!file_exists($file)) {
  92.                 return $this->raiseError("File does not exist: $fname");
  93.             } else {
  94.                 $filelist[$i++] = $file;
  95.                 if (empty($pkginfo['filelist'][$fname]['md5sum'])) {
  96.                     $md5sum = md5_file($file);
  97.                     $tpkginfo['filelist'][$fname]['md5sum'] = $md5sum;
  98.                 }
  99.                 $this->log(2, "Adding file $fname");
  100.             }
  101.         }
  102.         // }}}
  103.  
  104.         // {{{ regenerate package.xml
  105.         $new_xml = $this->xmlFromInfo($pkginfo);
  106.         if (PEAR::isError($new_xml)) {
  107.             return $this->raiseError($new_xml);
  108.         }
  109.         if (!($tmpdir = System::mktemp(array('-d')))) {
  110.             return $this->raiseError("PEAR_Packager: mktemp failed");
  111.         }
  112.         $newpkgfile = $tmpdir . DIRECTORY_SEPARATOR . 'package.xml';
  113.         $np = @fopen($newpkgfile, 'wb');
  114.         if (!$np) {
  115.             return $this->raiseError("PEAR_Packager: unable to rewrite $pkgfile as $newpkgfile");
  116.         }
  117.         fwrite($np, $new_xml);
  118.         fclose($np);
  119.         // }}}
  120.  
  121.         // {{{ TAR the Package -------------------------------------------
  122.         $ext = $compress ? '.tgz' : '.tar';
  123.         $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
  124.         $tar =& new Archive_Tar($dest_package, $compress);
  125.         $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
  126.         // ----- Creates with the package.xml file
  127.         $ok = $tar->createModify(array($newpkgfile), '', $tmpdir);
  128.         if (PEAR::isError($ok)) {
  129.             return $this->raiseError($ok);
  130.         } elseif (!$ok) {
  131.             return $this->raiseError('PEAR_Packager: tarball creation failed');
  132.         }
  133.         // ----- Add the content of the package
  134.         if (!$tar->addModify($filelist, $pkgver, $pkgdir)) {
  135.             return $this->raiseError('PEAR_Packager: tarball creation failed');
  136.         }
  137.         $this->log(1, "Package $dest_package done");
  138.         if (file_exists("$pkgdir/CVS/Root")) {
  139.             $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $pkginfo['version']);
  140.             $cvstag = "RELEASE_$cvsversion";
  141.             $this->log(1, "Tag the released code with `pear cvstag $pkgfile'");
  142.             $this->log(1, "(or set the CVS tag $cvstag by hand)");
  143.         }
  144.         // }}}
  145.  
  146.         return $dest_package;
  147.     }
  148.  
  149.     // }}}
  150. }
  151.  
  152. // {{{ md5_file() utility function
  153. if (!function_exists('md5_file')) {
  154.     function md5_file($file) {
  155.         if (!$fd = @fopen($file, 'r')) {
  156.             return false;
  157.         }
  158.         $md5 = md5(fread($fd, filesize($file)));
  159.         fclose($fd);
  160.         return $md5;
  161.     }
  162. }
  163. // }}}
  164.  
  165. ?>
  166.